package cn.edu.sdjzu.xg23.kcsj01_backend.controller;



import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Standard;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.StandardService;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/standard")
public class StandardController {
    @Autowired
    private StandardService standardService;
    @PreAuthorize("hasAuthority('standard:save')")
    @PostMapping("/standard")
    public Result save(@RequestBody Standard standard) {
        if (standardService.save(standard)) {
            return Result.success("增加成功");
        } else {
            return Result.error("200", "增加成功");
        }
    }

    @PreAuthorize("hasAuthority('standard:delete')")
    @DeleteMapping("/standard/{id}")
    public Result delete(@PathVariable Long id) {
        if (standardService.delete(id)) {
            return Result.success("删除成功");
        } else {
            return Result.error("500", "删除失败");
        }
    }
    @PreAuthorize ("hasAuthority('standard:update')")
    @PutMapping("/standard")
    public Result update(@RequestBody Standard standard) {
        if (standardService.update(standard)) {
            return Result.success("更新成功");
        } else {
            return Result.error("500", "更新失败");
        }
    }

    @PreAuthorize("hasAuthority('standard:search:all') or hasAuthority('standard:search:my')")
    @PostMapping("/search")
    public Result get(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody(required = false) Standard  standard,
            @RequestParam(required = false) Long employeeId) {
        PageInfo<Standard> pageInfo = standardService.get(pageNum, pageSize, standard,employeeId);
        return Result.success(pageInfo);
    }

    @PreAuthorize("hasAuthority('standard:approve')")
    @PutMapping("/approve")
    public Result approve(@RequestBody Standard standard) {
        if (standardService.approve(standard)) {
            return Result.success("审批成功");
        } else {
            return Result.error("500", "审批失败");
        }
    }
}